package p2p;

import java.util.List;
import java.util.LinkedList;

/** Help functions for use with the retrieve functionality
 * @author ei08150
 */

public class HelperGetFunctions {

    public static String constructGetCommand(String hash, int chunkNumber) {
        return "GET " + hash + " " + chunkNumber;
    }

    public static String constructGetCommand(String hash, List<Integer> chunks) {
        String return_value = "GET " + hash + " ";

		boolean first = true;
        for (int chunk : chunks) {
			if(!first)
				return_value += ",";
			else
				first = false;
            return_value += chunk;
        }

        return return_value;
    }

    public static String constructGetCommand(String hash, int leftValue, int rightValue) {
        return "GET " + hash + " " + leftValue + "-" + rightValue;
    }

    /** Indicates if str is a number
        @param str The string to check
        @return <b>true</b> if str is a number, otherwise return <b>false</b>
    */
    public static boolean isNumber(String str) {
        try {
            long temp = Long.parseLong(str);
        }
        catch (NumberFormatException e) {
            return false;
        }

        return true;
    }

    /** Indicates if str is a group of comma separated values and if
        the values are all numbers.
        @param str The string to check
        @return <b>true</b> if str is a CSV, otherwise return <b>false</b>
    */
    public static boolean isCSV(String str) {
        String[] splittedStr = str.split(",");

        for (String value : splittedStr) {
            if (!isNumber(value)) {
                return false;
            }
        }

        return true;
    }

    /** Convert a string representation of a list of comma separated
     * values to a actual list.
     * Assume the CSV is composed only of integers
     * @param str A string representation of a list of CSV
     * @return A list of integers
     */
    public static List<Integer> convertCSVToList(String str) {
        List<Integer> list = new LinkedList<Integer>();

        String[] splittedStr = str.split(",");

        // Assume that str is a valid CSV
        for (String value : splittedStr) {
            list.add(Integer.parseInt(value));
        }

        return list;
    }

    /** Indicates if str is a range where the left value is smaller
     * than the right value
     * @param str The string to check
     * @return <b>true</b> if str is a range, otherwise return <b>false</b>
     */
    public static boolean isRange(String str) {
        String[] splittedStr = str.split("-");

        if (splittedStr.length != 2)
            return false;

        String leftValue = splittedStr[0];
        String rightValue = splittedStr[1];

        if (!isNumber(leftValue))
            return false;
        if (!isNumber(rightValue))
            return false;

        // leftValue can't be larger than the rightValue

        long leftNumber = Long.parseLong(leftValue);
        long rightNumber = Long.parseLong(rightValue);

        if (leftNumber > rightNumber)
            return false;
        
        return true;
    }
}
